//
//  HLJMusicCell.m
//  B段项目
//
//  Created by 111111 on 15/12/22.
//  Copyright © 2015年 张智超. All rights reserved.
//

#import "HLJMusicCell.h"

@implementation HLJMusicCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createSubView];
    }
    return self;
}

#pragma mark - 子视图
-(void)createSubView{
    
    
    //类型
    self.type = [[UILabel alloc]init];
    [self.contentView addSubview:self.type];

    
    [self.type mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.size.mas_equalTo(CGSizeMake(120, 20));
        // 添加左、上边距约束（左、上约束都是20）
        make.left.and.top.mas_equalTo(20);
        
        
    }];

    self.type.font = [UIFont systemFontOfSize:WIDTH/25];
    self.type.textColor = [UIColor colorWithRed:0.47 green:0.47 blue:0.47 alpha:1];
    
    [self.type mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.size.mas_equalTo(CGSizeMake(120, 20));
        // 添加左、上边距约束（左、上约束都是20）
        make.left.and.top.mas_equalTo(20);
        
        
    }];
    
    //标题
    self.titleLb = [[UILabel alloc]init];
    
    self.titleLb.font = [UIFont systemFontOfSize:20];
    [self.contentView addSubview:self.titleLb];
  
    
    [self.titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.size.mas_equalTo(CGSizeMake(WIDTH - 20, 60));
        // 添加左、上边距约束（左、上约束都是20）
        make.left.mas_equalTo(20);
        make.top.mas_equalTo(60);
        
        
    }];
    
    //作者
    self.name = [[UILabel alloc]init];
    
    [self.contentView addSubview:self.name];
    [self.name mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.size.mas_equalTo(CGSizeMake(200, 20));
        // 添加左、上边距约束（左、上约束都是20）
        make.left.mas_equalTo(20);
        make.top.mas_equalTo(120);
        
        
    }];
    self.name.textColor = [UIColor colorWithRed:0.47 green:0.47 blue:0.47 alpha:1];
    //    self.name.backgroundColor = [UIColor yellowColor];
    self.name.font = [UIFont systemFontOfSize:WIDTH/25];
    
 
    
    
    //详情
    self.content = [[UILabel alloc]init];
      [self.contentView addSubview:self.content];
    
    [self.content mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.height.mas_equalTo(100);
        // 添加左、上边距约束（左、上约束都是20）
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(- 20);
        make.top.mas_equalTo(160);
        
        
    }];

    self.content.numberOfLines = 0;
    self.content.font = [UIFont systemFontOfSize:WIDTH/20];
    self.content.textColor = [UIColor colorWithRed:0.47 green:0.47 blue:0.47 alpha:1];
  
 
    
    
    
    //心形图
    self.heartButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.contentView addSubview: self.heartButton];
    
    [self.heartButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.size. mas_equalTo(CGSizeMake(30, 30));
        // 添加左、上边距约束（左、上约束都是20）
        make.bottom.mas_equalTo(-20);
        
        make.right.mas_equalTo(- 75);
        
        
        
    }];
    
    [self.heartButton  setImage:[UIImage imageNamed:@"iconfont-xinxihuan00-2.png"] forState:UIControlStateNormal];
    
     [self.heartButton addTarget:self action:@selector(heartButton:) forControlEvents:UIControlEventTouchUpInside];
    
    
    //赞数
    self.zanLabel = [[UILabel alloc]init];
    [self.contentView addSubview:self.zanLabel];
    
    [self.zanLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.size. mas_equalTo(CGSizeMake(35, 20));
        // 添加左、上边距约束（左、上约束都是20）
        make.bottom.mas_equalTo(-25);
        
        make.right.mas_equalTo(- 25);
        
        
        
    }];
    
    self.zanLabel.font = [UIFont systemFontOfSize:12];
    self.zanLabel.textColor = [UIColor colorWithRed:0.47 green:0.47 blue:0.47 alpha:1];
    
    
    
}

#pragma mark - set
-(void)setMusicModel:(HLJHomeModel *)musicModel{
    _musicModel = musicModel;
    self.type.text = [NSString stringWithFormat:@"%@ · %@",self.musicModel.name,self.musicModel.enname];
    self.content.text = musicModel.content;
    self.zanLabel.text = [NSString stringWithFormat:@"%@",self.musicModel.like];
    self.titleLb.text = self.musicModel.title;

    self.name.text = [NSString stringWithFormat:@"by:%@ 主播",[self.musicModel.userinfo objectForKey:@"uname"]];
    
    //判断收藏状态
    NSMutableArray *array = [[ZZCCollectionData collectDataBase]selectCollectList];
    NSMutableArray *array2 = [NSMutableArray array];
    for (HLJHomeModel *model in array) {
        [array2 addObject:model._id];
    }
    if ([array2 containsObject: self.musicModel._id]) {
        [self.heartButton  setImage:[UIImage imageNamed:@"iconfont-xinxihuan002.png"] forState:UIControlStateNormal];
    }
    
}

- (void)heartButton:(UIButton *)btn
{
    NSMutableArray *array = [[ZZCCollectionData collectDataBase] selectCollectList];
    NSMutableArray *amptyArray = [NSMutableArray array];
    
    for (HLJHomeModel *model in array) {
        [amptyArray addObject:model._id];
    }
    if ([amptyArray containsObject:self.musicModel._id]) {
        [[ZZCCollectionData collectDataBase] deleteMyCollectData:self.musicModel.title];
        
        CAKeyframeAnimation *keyAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
        keyAnimation.values = @[@(0.1),@(1.0),@(1.5)];
        keyAnimation.duration = 0.2;
        [btn.layer addAnimation:keyAnimation forKey:@"key"];
        [self.heartButton  setImage:[UIImage imageNamed:@"iconfont-xinxihuan00-2.png"] forState:UIControlStateNormal];
        self.zanLabel.text = [NSString stringWithFormat:@"%ld",[(self.zanLabel.text)integerValue] - 1];
    }else{
        HLJHomeModel *model = [[HLJHomeModel alloc]init];
        model._id = self.musicModel._id;
        model.title = self.musicModel.title;
        model.coverimg = self.musicModel.coverimg;
        model.name = self.musicModel.name;
        
        [[ZZCCollectionData collectDataBase]inserCollectList:model];
        
        //关键帧动画
        CAKeyframeAnimation *keyAnimtion = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
        //设置关键帧的值,values是数组类型
        //@(0.1)相当于NSNumber
        keyAnimtion.values = @[@(0.1),@(1.0),@(1.5)];//缩放的倍数
        keyAnimtion.duration = 0.2;
        [btn.layer addAnimation:keyAnimtion forKey:@"key"];
        
        
        [self.heartButton  setImage:[UIImage imageNamed:@"iconfont-xinxihuan002.png"] forState:UIControlStateNormal];
        self.zanLabel.text = [NSString stringWithFormat:@"%ld",[(self.zanLabel.text)integerValue] + 1];
    }
    
    
    
}




@end
